package com.jun.staffsystem.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jun.staffsystem.common.Result;
import com.jun.staffsystem.entity.Department;

import com.jun.staffsystem.mapper.DepartmentMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/*
 * @Author: jun
 * @Date:2022/11/28 10:57
 * @概述：
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Resource
    DepartmentMapper departmentMapper;

    /**
     *
     * @param department
     * @return
     */
    @PostMapping
    public Result<?> add(@RequestBody Department department){
        departmentMapper.insert(department);
        return Result.success();
    }

    /**
     * 分页号码
     * @param pageNum
     * @param pageSize
     * @param search
     * @return
     */
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){

        LambdaQueryWrapper<Department> wrapper = Wrappers.<Department>lambdaQuery();
        if (StrUtil.isNotBlank(search)){
            wrapper.like(Department::getDepartmentName, search);
        }
        Page<Department> p = departmentMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(p);
    }


    /**
     *
     * @param department
     * @return
     */
    @PutMapping
    public Result<?> update(@RequestBody Department department){
        departmentMapper.updateById(department);
        return Result.success();
    }

    /**
     *
     * @param did
     * @return
     */
    @DeleteMapping("/{did}")
    public Result<?> delete(@PathVariable Long did){
        departmentMapper.deleteById(did);
        return Result.success();
    }
}
